<?php
  
  class rTorrentConnection {
  
    private $xmlrpc_client;
    
    public function rTorrentConnection() {
        $this->xmlrpc_client = new xmlrpc_client("RPC2/", "localhost", 80);
        
  //       if(RT_AUTH)
  //         $this->client->setCredentials(RT_USER, RT_PASSWD);
        
        $this->xmlrpc_client->return_type = "phpvals";
        $this->xmlrpc_client->no_multicall = true;
        
        $message = new xmlrpcmsg("system.pid");
        $result = $this->xmlrpc_client->send($message);
        
        return ($result->errno == 0);
    }
    
    public function getTorrents() {
        
        $request = array( "default",
                                  "d.get_hash=",
                                  "d.get_name=",
                                  "d.get_down_rate=",
                                  "d.get_up_rate=",
                                  "d.get_completed_chunks=",
                                  "d.get_size_chunks=",
                                  "d.get_state=",
                                  "d.is_open=",
                                  "d.is_hash_checking=");
        
        foreach ($request as $param)
          $array_post[] = new xmlrpcval($param, "string");
        $response = $this->xmlrpc_client->send(new xmlrpcmsg("d.multicall", $array_post));
        
        $torrents = array();
        
        foreach ($response->val as $item) {
          $details["hash"] = $item[0];
          $details["name"] = $item[1];
          $details["down_rate"] = round($item[2]/1024,2);
          $details["up_rate"] = round($item[3]/1024,2);
          $details["completed_chunks"] = $item[4];
          $details["size_in_chunks"] = $item[5];
          $details["chunk_size"] = $item[4];
          $details["state"] = $item[6];
          $details["open"] = $item[7];
          $details["hashchecking"] = $item[8];
          $torrents[] = new Torrent($details);
        }
        
        return $torrents;
    }
    
    public function getTorrent($hash) {
      
      $request = array( "d.get_name",
                                "d.get_down_rate",
                                "d.get_up_rate",
                                "d.get_completed_chunks",
                                "d.get_size_chunks",
                                "d.get_chunk_size",
                                "d.get_ratio",
                                "d.get_peers_connected",
                                "d.get_peers_not_connected",
                                "d.get_peers_complete",
                                "d.get_state",
                                "d.is_open",
                                "d.is_hash_checking");
      
      foreach($request as $param)
        $array_post[] = new xmlrpcmsg($param, array(new xmlrpcval($hash, "string")));
      $response = $this->xmlrpc_client->multicall($array_post);
      
      $details["name"] = $response[0]->val;
      $details["down_rate"] = round(($response[1]->val)/1024,2);
      $details["up_rate"] = round(($response[2]->val)/1024,2);
      $details["completed_chunks"] = $response[3]->val;
      $details["size_in_chunks"] = $response[4]->val;
      $details["chunk_size"] = $response[5]->val;
      $details["ratio"] = round($response[6]->val/1000,2);
      $details["peers"] = $response[7]->val;
      $details["seeds"] = $response[9]->val;
      
      $details["state"] = $response[10]->val;
      $details["open"] = $response[11]->val;
      $details["hashchecking"] = $response[12]->val;
      
      return new Torrent($details);
    }
    
  }

?>